package com.zhuss.core.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.permission.DeletePermissionResourceBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.PermissionResourceDao;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.EnumUtil;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.PermissionResourceDeleteType;
import com.zhuss.exception.AabException;
import com.zhuss.util.JsonUtil;

/**
 * 解除权限资源关系 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("permissionResourceDelete")
public class PermissionResourceDelete extends AbstractProcessor<Object, DeletePermissionResourceBo> implements Processor<Object, DeletePermissionResourceBo> {

    @Autowired
    private PermissionResourceDao permissionResourceDao;
    
    
    /**
     * 校验
     */
    @Override
    public void check(DeletePermissionResourceBo bo) throws AabException {
    	
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(DeletePermissionResourceBo bo) {
    	Integer type = bo.getType();
    	Integer checkType = bo.getCheckType();
    	if (PermissionResourceDeleteType.根据权限删除权限资源关系.sameKey(type)) {
    		Long permissionId = bo.getPermissionId();
    		List<Long> resourceIdList = bo.getResourceIdList();
    		permissionResourceDao.deleteByPermissionResourceByType(bo, permissionId, resourceIdList, checkType);
    	} else if (PermissionResourceDeleteType.根据资源删除权限资源关系.sameKey(type)) {
    		Long resourceId = bo.getResourceId();
    		List<Long> permissionIdList = bo.getPermissionIdList();
    		permissionResourceDao.deleteByResourcePermission(bo, resourceId, permissionIdList);
    	}
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DeletePermissionResourceBo param) throws AabException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.权限点资源关系解除.key());
    	record.setTargetId(param.getType().longValue());
    	record.setTargetName(EnumUtil.containsKey(PermissionResourceDeleteType.class, param.getType()));
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
		return null;
	}
}
